package splug.cjk;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

import splug.cjk.packet.FriendPacket;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class PersonInfo extends Activity {
	
	private String id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.person_info);
		setTitle("정보 보기");
		
		Bundle b = this.getIntent().getExtras();
		id = b.getString("ID");

		TextView tv_id = (TextView) findViewById(R.id.tv_personinfo_id);
		TextView tv_name = (TextView) findViewById(R.id.tv_personinfo_name);
		TextView tv_car = (TextView) findViewById(R.id.tv_personinfo_carnum);
		TextView tv_phone = (TextView) findViewById(R.id.tv_personinfo_phone);
		TextView tv_mail = (TextView) findViewById(R.id.tv_personinfo_mail);
		TextView tv_report = (TextView) findViewById(R.id.tv_personinfo_report);
		Button back = (Button) findViewById(R.id.btn_personinfo_back);
		
		//뒤로가기 버튼 이벤트 추가
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		//23번 요청은 정보보기 요청이다
		try {
			Socket socket = new Socket(LetsGoActivity.server_ip, 2007);

			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();

			OutputStreamWriter osw = new OutputStreamWriter(os);
			InputStreamReader isr = new InputStreamReader(is);

			osw.write(23);
			osw.flush();

			int ack = isr.read(); 

			if(ack == '1'){
				FriendPacket packet = new FriendPacket(null, id, null);
				ObjectOutputStream output = new ObjectOutputStream(os);
				output.writeObject(packet);

				ack = isr.read();

				//정보를 넘겨받고 화면에 출력한다
				if(ack == '1'){
					Toast.makeText(PersonInfo.this, id + "님의 정보입니다.", Toast.LENGTH_SHORT).show();
					ObjectInputStream input = new ObjectInputStream(is);
					packet = (FriendPacket) input.readObject();
					tv_id.setText(packet.getFriend_list().get(0));
					tv_name.setText(packet.getFriend_list().get(1));
					tv_car.setText(packet.getFriend_list().get(2));
					tv_phone.setText(packet.getFriend_list().get(3));
					tv_mail.setText(packet.getFriend_list().get(4));
					tv_report.setText(packet.getFriend_list().get(5));
				}
				else if(ack == '0'){
					Toast.makeText(PersonInfo.this, "해당하는 회원을 찾을 수 없습니다", Toast.LENGTH_SHORT).show();
				}
			}
			else{
				Toast.makeText(PersonInfo.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
				finish();
			}


		} catch (Exception e) {
			Toast.makeText(PersonInfo.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
			finish();
		}
	}

}
